﻿using System;

public class BaseState<T> : BaseMono, IState
{
	public T owner;

	#region State

	public string Name
	{
		get { return Data.StateName; }
	}

	public int priority;
	public int Priority {
		get { return priority; }
		protected set { priority = value; }
	}

	public bool isDoing;
	public bool IsDoing {
		get {
			return isDoing;
		}
		set {
			isDoing = value;
		}
	}

	public DateTime StartTime;

	public StateData Data;

	public virtual void Init(T t)
	{
		owner = t;
		base.Init();
	}

	public virtual void OnEnter(object args = null)
	{
		isDoing = true;
		StartTime = DateTime.Now;
		MyLog.Log("Enter : " + Name);
	}

	public override void onFixedUpdate()
	{
		if (isDoing)
		{
			OnStateFixedUpdate();
		}
	}

	public virtual void OnStateFixedUpdate(object args = null)
	{
	}

	public override void onUpdate()
	{
		if (isDoing)
		{
			OnStateUpdate();
		}
	}

	public virtual void OnStateUpdate(object args = null)
	{
	}

	public override void onLateUpdate()
	{
		if (isDoing)
		{
			OnStateLateUpdate();
		}
	}

	public virtual void OnStateLateUpdate(object args = null)
	{
	}

	public virtual void OnExit(object args = null)
	{
		MyLog.Log("Exit : " + priority);
		isDoing = false;
	}

	public virtual bool IsComplete()
	{
		return (DateTime.Now - StartTime).TotalMilliseconds > Data.DurationTime;
	}

	#endregion
}

[Serializable]
public class StateData
{
	/// <summary>
	/// 状态名称
	/// </summary>
	public string StateName;

	/// <summary>
	/// 状态持续时间(ms)
	/// </summary>
	public float DurationTime;

	public StateData(string name, float time = 0)
	{
		StateName = name;
		DurationTime = time;
	}
}

